<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>活动详情</title>
<link href="<%=path%>/favicon.ico" rel="shortcut icon">



<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.css">


</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-12 text-right">
			   <i class="fa fa-times" onclick="javascript:colseModal()"></i>
			</div>
			<div class="col-md-12 text-center" style="width:100%">
			   <h4>选择门店</h4>
			</div>
		    <div class="col-md-12" style="margin-top:15px">
		       <form action="" class="form-inline col-sm-6">
				    <div class="form-group">
				       <label>关键字</label>
				       <input class="form-control" id="bm_keyword" type="text" placeholder="输入关键字" maxlength="20" onblur="checkLength(this)">
				    </div>
				    <div class="form-group" style="margin-left: 15px;">
				        <button id="bm_csous" onclick="javascript:getStoreList();"  type="button" class="btn btn-primary">搜索
	                    </button>
				    </div>
			   </form>
			   <div class=" text-right col-sm-6">
				   <!-- <button id="add_store" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal6">添加门店
	               </button> -->
			   </div>
		    </div>
		    <div class="col-md-12">
		       <div class="ibox-content col-md-12" style="border:none;">
	                <table class="table table-bordered">
	                    <thead>
	                        <tr>
                                <th data-toggle="true" style="text-align:left"><img src="<%=path%>/images/marketing/s_check_no.png" class="x_CheckBox_pic" _id="27" />&nbsp;&nbsp;门店名称</th>
                                <th style="text-align:left">地址</th>
                                <th style="text-align:left" data-hide="all">电话</th>
                                <!-- <th style="text-align:left">操作</th> -->
                            </tr>
	                    </thead>
	                    <tbody id = "bm_cstore">
	                    </tbody>
	                </table>
               </div>
		    </div>
		    <div class="m_xpages" style="width:100%;margin:0;padding: 2px 0 1px 0;">
				<div class="mian">
					<span>显示行数</span> <select><option>5</option>
						<option>10</option>
						<option>20</option></select> <span>共80条</span> <span>1/4页</span> <a
						href="javascript:;" class="prev">上一页</a> <a href="javascript:;"
						class="next">下一页</a> <span>前往</span> <input type="text" value="1"
						class="text"> <span>页</span>
				</div>
			</div>
		    <div class="col-md-12 text-center" style="margin-top:40px;width:100%">
		        <div class="col-md-3"></div>
		        <button type="button" onclick="javascript:colseModal();" class="col-md-2 btn btn-w-m btn-outline btn-primary">取消</button> 
		        <div class="col-md-2"></div>
			    <button type="button" onclick="javascript:determineStore();" class="col-md-2 btn btn-w-m btn-primary">确定</button> 
			    <div class="col-md-3"></div>
		    </div>
	    </div>			
	</div>
</div>

<!-- 添加门店模态框 -->
<div class="modal inmodal fade" id="myModal6" tabindex="-1" role="dialog"  aria-hidden="true">
    <div class="modal-dialog modal-sm">
       <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" onclick="javascript:closeAddModal();"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">添加门店</h4>
            </div>
            <div class="modal-body">
			    
               <form  id="commentForm" method="get" class="form-horizontal m-t">
		         <div id="form_name" class="form-group">
		             <label class="col-sm-4 control-label">门店名称</label>
		             <div class="col-sm-8">
		                 <input id="name" type="text" class="form-control" oninput="removeError()" maxlength="20" onblur="checkLength(this)">
		             </div>
		         </div>
		         <div id="form_address" class="form-group">
		             <label class="col-sm-4 control-label">门店地址</label>
		             <div class="col-sm-8">
		                 <input id="address" type="text" class="form-control" oninput="removeError()" maxlength="25" onblur="checkLength(this)">
		             </div>
		         </div>
		         <div class="form-group">
		             <label class="col-sm-4 control-label">电话</label>
		             <div class="col-sm-8">
		                 <input id="store_phone" type="text" class="form-control" onkeypress="keyPress()" maxlength="11"">
		             </div>
		         </div>
		         
		     </form>
			     
            </div>

            <div class="modal-footer">
                <button id="add_close" onclick="javascript:closeAddModal();" type="button" class="btn btn-white" >关闭</button>
                <button onclick="javascript:save_or_edit();" type="button" class="btn btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>






    
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>     
<script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<%-- <script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script> --%>
<script type="text/javascript" src="<%=path%>/js/marketing/dialog.js"></script>   
<script type="text/javascript">
   var address="";
   var name="";
   var phone="";
   var key_word="";


	//分页
   var startIndex = 0;
   var pageSize = 5;
   var currentPage = 1;
   var pageCount = 0;
   //标记是编辑门店 还是添加门店
   var flag=1;
   
   $(function(){
	   getStoreList();
   });
   
   //查询所有门店
   function getStoreList(){
   	//获取关键字
   	key_word = $('#bm_keyword').val();
   	var url = "<%=path%>/store/ajaxStorePageList";
   	$.post(url,{
			"keyWord" : key_word,
			"startIndex" : startIndex,
			"pageSize" : pageSize ,
   	},function(data){
   		
   		if(data.status == 200){
   			page(data.totalCount);
   			storeHtml(data.data);
   		}else if(data.status == 300){
   			$('#bm_cstore').html("");
   			page(0);
   		}
   		
   	});
   } 
    
   function save_or_edit(){
	   if(flag==1){
		   saveStore();
	   }else if(flag==2){
		   editStore();
	   }
   }
   
   
   function closeAddModal(){
	   $('#myModal6').modal("hide");
	   clearStoreInfo();
   }
   
   
   function clearStoreInfo(){
	   $('#name').val("");
	   $('#address').val("");
	   $('#store_phone').val("");
   }
    //添加门店
    function saveStore(){
    	phone=$('#store_phone').val();
    	address=$('#address').val();
    	name=$('#name').val();
    	if(StringEmpty(phone)&&!checkTel(phone)){
    		errorDialog("","电话输入不正确，请重新输入！");
    		return ;
    	}
    	if(StringEmpty(address)&&StringEmpty(name)){
    		var url = "<%=path%>/store/ajaxSaveStore";
            $.post(url,{
      		  "name" : name,
      		  "address" : address,
      		  "phone" : phone,
            },function(data){
          	  
          	  if(data.status == "200"){
          		  getStoreList();
          		 $('#myModal6').modal('toggle');
          		 successDialog('添加成功',"");
          		 clearStoreInfo();
          		 
          	  }else if(data.status == "301"){
    	      	 errorDialog('该门店已存在，请勿重复创建',"");
        	  }else{
          		 errorDialog('添加门店失败',"");
          	  }
            });
    	}else if(!StringEmpty(name)){
    		$('#form_name').addClass("has-error");
    	}else{
    		$('#form_address').addClass("has-error");
    	}
    }
    //编辑门店
    function editStore(){
    	//获取编辑的名称
    	var name = $('#name').val();
    	//获取编辑的地址
    	var address = $('#address').val();
    	//获取编辑的电话
    	var phone = $('#store_phone').val();
    	if(StringEmpty(phone)&&!checkTel(phone)){
    		errorDialog("","电话输入不正确，请重新输入！");
    		return ;
    	}
    	if(StringEmpty(name)&&StringEmpty(address)){
    	var url = "<%=path%>/store/ajaxUpdateStore";
    	$.post(url,{
			"id" : storeId,
			"name" : name,
			"address" : address,
			"phone" : phone,
    	},function(data){
    		
    		if(data.status == 200){
    			getStoreList();
    			$('#myModal6').modal('toggle');
    			successDialog("编辑成功!","");
    			clearStoreInfo();
    		}else if(data.error_code == "4"){
    			errorDialog("该门店已存在，请勿重复编辑！");
    		}else{
    			errorDialog("编辑失败，请重试！");
    		}
    	 });
    	}else if(!StringEmpty(name)){
    		$('#form_name').addClass("has-error");
        }else if(!StringEmpty(address)){
        	$('#form_address').addClass("has-error");
        }
    }
    //删除门店
    function deleteStore(){
    	var url = "<%=path%>/store/ajaxDeleteStore";
    	var lineId = '#line'+storeeId;
    	$.post(url,{
			"id" : storeeId,
    	},function(data){
    		
    		if(data.status == 200){
    			getStoreList();
    			$(lineId).remove();
    			successDialog("删除成功","");
    		}else if(data.status == "2"){
    			errorDialog("该门店已被选用，请勿删除","");
    		}else if(data.status == "1"){
    			errorDialog("删除失败","");
    		}else{
    			errorDialog("删除失败","");
    		}
    	});
    }
    
    function storeHtml(data){
    	var html = "";
    	var tag=false;
    	var selectCount=0;
    	for(var i=0;i<data.length;i++){
    		html += '<tr id = "line'+data[i].id+'">'
    		if(store_ids.length<1){
    			html += '<td><div title="'+data[i].name+'" style="width:14em; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><img id="'+data[i].id+'" src="<%=path%>/images/marketing/s_check_no.png" class="x_CheckBox" _id="27" address="'+data[i].address+'" cname="' + data[i].name + '"/>&nbsp;&nbsp;' + data[i].name + '</div></td>';	
    		}else{
    			for(var j=0;j<store_ids.length;j++){
	    			if(data[i].id==store_ids[j]){
	    				tag=true;
	    				selectCount++;
	    			}
	    		}
    			if(tag){
    				html += '<td><div title="'+data[i].name+'" style="width:14em; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><img id="'+data[i].id+'" src="<%=path%>/images/marketing/s_check.png" class="x_CheckBox" _id="27" address="'+data[i].address+'" cname="' + data[i].name + '"/>&nbsp;&nbsp;' + data[i].name + '</div></td>';
    			}else{
    				html += '<td><div title="'+data[i].name+'" style="width:14em; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><img id="'+data[i].id+'" src="<%=path%>/images/marketing/s_check_no.png" class="x_CheckBox" _id="27" address="'+data[i].address+'" cname="' + data[i].name + '"/>&nbsp;&nbsp;' + data[i].name + '</div></td>';	
    			}
    		}
        	html += '<td><div title="'+data[i].address+'" style="width:14em; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' + data[i].address + '</div></td>';
        	html += '<td><div title="'+data[i].address+'" style="width:14em; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' + data[i].phone + '</div></td>';
        	/* html += '<td class="x_color_a"><a href="javascript:;" class="x_editor_bj" cphone="'+data[i].phone+'" caddress="'+data[i].address+'" cname="'+data[i].name+'" sid="'+data[i].id+'"><font color="#18a689">编辑</font></a> <a class="deleted" href="javascript:;" cid = "'+data[i].id+'"><font color="#18a689">删除</font></a></td></tr>'; */
    	    tag=false;
    	}
    	if(selectCount==data.length){
    		$(".x_CheckBox_pic").attr('src', '<%=path%>/images/marketing/s_check.png');
            $(".ibox-content").find(".x_CheckBox").attr('src', '<%=path%>/images/marketing/s_check.png');
    	}else{
    		$(".x_CheckBox_pic").attr('src', '<%=path%>/images/marketing/s_check_no.png');
            $(".ibox-content").find(".x_CheckBox").attr('src', '<%=path%>/images/marketing/s_check_no.png');
    	}
    	$('#bm_cstore').html(html);
     }
    
    function removeError(){
    	$('.form-group').removeClass("has-error");
    }
    
    /* 分页 */
    $(".m_xpages select").change(function(){
 		pageSize = Number($(this).val());	
 		startIndex=0;
 		getStoreList();
 	});
 	
    $(".m_xpages a.prev").click(function(){	 		
 		$('.next').html('<font color="black">下一页</font>');
 		if(currentPage > 1){
 			currentPage--;
 			startIndex -= pageSize;	 
 			if(currentPage == 1){
	 			$('.prev').html('<font color="#E0E0E0">上一页</font>');
 			}
	 			getStoreList();
 		}
 	});
 	
 	$(".m_xpages a.next").click(function(){
 		$('.prev').html('<font color="black">上一页</font>'); 
 		if(currentPage < pageCount){
 			currentPage++;
 			startIndex += pageSize;
 			if(currentPage == pageCount){
	 			$('.next').html('<font color="#E0E0E0">下一页</font>');
	 		}
	 			getStoreList();
 		}
 	});
 	
 	$(".m_xpages :text").keyup(function(e){
		if(e.keyCode==13){
			var pageGo = $(this).val();
			if(/^[0-9]+$/.test(pageGo)){
				pageGo = Number(pageGo);
				if(pageGo<1 || pageGo>pageCount){
					errorDialog('请输入合法页数',"");
				}else if(pageGo != currentPage){
					startIndex = (pageGo -1) * pageSize ;	
					getStoreList();
				}
			}else{
				errorDialog('请输入合法页数',"");
			}
		}
	});
 	
  function page(totalCount) {
	pageCount = totalCount/pageSize;
		if (totalCount % pageSize > 0)
			pageCount++;
		pageCount = parseInt(pageCount);
		currentPage =  startIndex / pageSize + 1;
		currentPage = parseInt(currentPage);
		if(currentPage == 1){
			$(".prev").addClass("pageDisable");
		}else{
			$(".prev").removeClass("pageDisable");
		}
		
		if(currentPage == pageCount || pageCount == 0){
			$(".next").addClass("pageDisable");
		}else{
			$(".next").removeClass("pageDisable");
		}
		
		$(".m_xpages>.mian>span:eq(1)").text('共'+totalCount+'条');
		$(".m_xpages>.mian>span:eq(2)").text(currentPage+'/'+pageCount+'页');
     }
    
    //判断字符串是否为空
	function StringEmpty(content) {
		if (content == undefined || content == "" || content == null) {
			return false;
		} else {
			return true;
		}
	}
		
</script>
<script type="text/javascript">




//删除门店
$(document).on('click','.deleted',function(){
	$('.s_popup_delect,.issue_delect').show();
    storeeId = $(this).attr("cid");
	deleteStore();
}); 
//勾选
//勾选


$('#add_store').click(function(){
	flag=1;
});


//勾选后点击确定
$('#bm_queque').click(function(){
	$('.x_popup_add,.issue_add').hide();
	store_names.length=0;
	store_ids.length=0;
	$('#bm_md_list').html('');
	$("#bm_cstore .x_CheckBox[src$='s_check.png']").each(
			function(i, d) {
				var name = $(this).attr("cname");
				store_names[i]=name;
				store_ids[i]=this.id;
				console.log("name--->"+name);
	});
	var html='';
	for(var i in store_names){
		html += '<li id="store_line'+i+'" indexNum="'+i+'" class="delete_md_detail" cid="'+store_names[i]+'" >'+store_names[i]+'<img indexNum="'+i+'" cid="'+store_names[i]+'" id="delete_store" src="<%=path%>/images/marketing/close.png" class="x_lista"></li>';
	}
	$('#bm_md_list').html(html);
});
//编辑时获取门店id 
$(document).on('click','.x_editor_bj',function(){
	flag=2;
	storeId = $(this).attr("sid");
	var name=$(this).attr("cname");
	var address=$(this).attr("caddress");
	var phone=$(this).attr("cphone");
	console.log("---------->"+name);
	$('#name').val(name);
	$('#address').val(address);
	$('#store_phone').val(phone);
	$('#myModal6').modal('show');
});


$('#myModal6').on('hidden.bs.modal', function () {
	if(!$('body').hasClass('modal-open')){
    	$("#parentBody").addClass('modal-open');
	} 
	
})
//监听
$('#myModal6').on('hidden.bs.modal', function () {
	if(!$('body').hasClass('modal-open')){
    	$("#parentBody").addClass('modal-open');
	} 
	
})

//全选
$(".x_CheckBox_pic").click(function () {
    var cutsrc = $(".x_CheckBox_pic").attr('src');
    if (cutsrc == "<%=path%>/images/marketing/s_check_no.png") {
        $(".x_CheckBox_pic").attr('src', '<%=path%>/images/marketing/s_check.png');
        $(".ibox-content").find(".x_CheckBox").attr('src', '<%=path%>/images/marketing/s_check.png');
    } else {
        $(".x_CheckBox_pic").attr('src', '<%=path%>/images/marketing/s_check_no.png');
        $(".ibox-content").find(".x_CheckBox").attr('src', '<%=path%>/images/marketing/s_check_no.png');
    }
    
    store_names.length=0;
	store_ids.length=0;
	$("#bm_cstore .x_CheckBox[src$='s_check.png']").each(
			function(i, d) {
				var name = $(this).attr("cname");
				store_names[i]=name;
				store_ids[i]=this.id;
	});
});

function checkTel(tel)
{
   var mobile = /^1[3|5|8]\d{9}$/ , phone = /^0\d{2,3}-?\d{7,8}$/;
   return mobile.test(tel) || phone.test(tel);
}

</script> 


</body>
</html>